package cn.echoedu.daoimpl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;

import cn.echoedu.dao.DAOFactory;
import cn.echoedu.dao.DAOUpdate;
import cn.echoedu.daoconstants.TableName;
import cn.echoedu.specifics.SpecMySql;
import cn.echoedu.util.DbUtil;
import cn.echoedu.util.L;


public class MySqlUpdate implements DAOUpdate{
	private final String T="MySqlUpdate-------->";
	DAOFactory fac=null;
	public MySqlUpdate(DAOFactory fac){
		this.fac=fac;
	}
	public <T> boolean updateInfo(Connection con, TableName tableName, T oldPojo,
			T currentPojo) throws SQLException {
		// TODO Auto-generated method stub
		final String M="updateInfo";
		PreparedStatement ps=null;
		try{
			ps=(PreparedStatement) SpecMySql.<T>getPreparedUpdate(con,tableName,oldPojo,currentPojo);
			L.p("mysql:"+ps.toString());
			ps.execute();
            con.commit();

		}catch(Throwable e){  
			if(con!=null){  
				try {  
					DbUtil.rollback(con);
				} catch (SQLException e1) {  
					e1.printStackTrace();  
				}  
			}  
		}finally{
			DAOFactory.closeAll(con,ps, null);
		}
		return true;
	}
	public boolean updateByColumn(Connection connection,String sql) {
		// TODO Auto-generated method stub\
		final String M="updateByColumn********>>";
		L.p(T+M);
		Statement statement = null;
		try {
			L.p("mysql:"+sql);
			statement = connection.createStatement();
			int result= statement.executeUpdate(sql);
			connection.commit();
			L.p(T+M+"update result is"+result);
			return result>0?true:false;
		} catch(Throwable e){  
			if(connection!=null){  
				try {  
					DbUtil.rollback(connection);
				} catch (SQLException e1) {  
					e1.printStackTrace();  
				}  
			}  
		}finally{
			DAOFactory.closeAll(connection,statement, null);
		}
		return false;


	}

}
